package jwdt.xadmin.goods.service.impl;

import jwdt.common.dao.BaseDao;
import jwdt.xadmin.goods.bean.TGoods;
import jwdt.xadmin.goods.bean.TGoodsUse;
import jwdt.xadmin.goods.service.GoodsUseManageService;

import com.ezerg.db.ext.PageUtil;

public class GoodsUseManageServiceImpl implements GoodsUseManageService {
	
	private BaseDao<TGoodsUse> baseDao;

	public void setBaseDao(BaseDao<TGoodsUse> baseDao) {
		this.baseDao = baseDao;
	}
	
	@Override
	public PageUtil selectGoodsUseList(PageUtil pu)throws Exception{
		String querySQL ="select tg.useid,tu.UserName username,t.name goodsname,tg.num,tg.dt,tg.zt from t_goodsuse tg left JOIN t_user tu on tu.userid = tg.userid left JOIN t_goods t on t.goodsid = tg.goodsid order by dt desc ";
		pu.doList(querySQL);
		return pu;
	}
	
	@Override
	public TGoodsUse selectOneGoodsUse(int useid)throws Exception{
		return baseDao.doLoadObj(TGoodsUse.class, useid);
	}
	
	@Override
	public String saveGoodsUse(int useid,TGoodsUse obj,TGoods tgoods)throws Exception{
		String ret = "";
		if(tgoods.getNum()<obj.getNum()){
			ret="物品数量不足";
		}else{
			if(useid==0){
				ret =  baseDao.doAddObj(obj);
			}else{
				ret = baseDao.doUpdateObj(TGoodsUse.class, useid, obj);
			}
			baseDao.doIntUpdateState(TGoods.class, "goodsid="+tgoods.getGoodsid(), "num="+(tgoods.getNum()-obj.getNum()));
		}
		return ret;
	}
	
	@Override
	public String delGoodsUse(int useid)throws Exception{
		return baseDao.doDelObj(TGoodsUse.class, useid);
	}
	
	@Override
	public String goodsUseChkact(int useid,int zt)throws Exception{
		return baseDao.doIntUpdateState(TGoodsUse.class, "useid="+useid, "zt="+zt);
	}

	@Override
	public PageUtil selectGoodsUseApplyList(PageUtil pu)throws Exception{
		String querySQL ="select tg.useid,tu.UserName username,t.name goodsname,tg.num,tg.dt,tg.zt from t_goodsuse tg left JOIN t_user tu on tu.userid = tg.userid left JOIN t_goods t on t.goodsid = tg.goodsid where tg.zt=0 order by dt desc ";
		pu.doList(querySQL);
		return pu;
	}
	
	@Override
	public PageUtil selectGoodsUseLogList(PageUtil pu)throws Exception{
		String querySQL ="select tg.useid,tu.UserName username,t.name goodsname,tg.num,tg.dt,tg.zt from t_goodsuse tg left JOIN t_user tu on tu.userid = tg.userid left JOIN t_goods t on t.goodsid = tg.goodsid where tg.zt=1 order by dt desc ";
		pu.doList(querySQL);
		return pu;
	}

}
